package Service;


import Dao.EmployeeDao;
import Dto.Employee;
import MyException.MyException;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class EmployeeService {
    EmployeeDao employeeDao = new EmployeeDao();
    //列表和分页信息
    public Map<String,Object> selectEmployeeByPage(Integer pageNum) throws SQLException {
        List<Employee> list = employeeDao.selectEmployList(pageNum);
        Long totalCount = employeeDao.selectEmployeeCount();
        Long totalPageNum = totalCount==0?1:(totalCount%10==0?totalCount/10:totalCount/10+1);
        Map<String,Object> map = new HashMap<>();
        map.put("list",list);
        map.put("totalCount",totalCount);
        map.put("totalPageNum",totalPageNum);
        return map;
    }
    //添加
    public void addEmployee(Employee employee) throws SQLException {
        employeeDao.addEmployee(employee);
    }
    //员工预修改
    public Employee preUpdateEmployee(Integer id) throws SQLException {
        Employee employee = employeeDao.preUpdateEmployee(id);
        return employee;
    }
    //员工修改
    public void updateEmployee(Employee employee) throws SQLException {
        employeeDao.updateEmployee(employee);
    }
    //员工删除
    public void deleteEmployee(Integer id) throws SQLException {
        employeeDao.deleteEmployee(id);
    }
    //四种查询
    public Map<String,Object> selectAllEmployee(String name,String idcard,String startDate,String endDate,String status) throws SQLException, MyException {
        Map<String, Object> map = new HashMap<>();
        //每个查询都需要查询总数和总页数(分页)
        //1.按照名字模糊查询
        if (name!=null&& !name.equals("")) {
            List<Employee> nameList=employeeDao.selectName(name);
            Long totalCount = Long.valueOf(nameList.size());
            Long totalPageNum=(totalCount==0)?1:((totalCount%10==0)?totalCount/10:totalCount/10+1);
            map.put("totalCount", totalCount);
            map.put("totalPageNum", totalPageNum);
            map.put("list", nameList);
            return map;
        }
        //2.按照身份证号查询

        if (idcard!=null&&!idcard.equals("")) {
            List<Employee> idcardList=employeeDao.selectIdCard(idcard);
            Long totalCount = Long.valueOf(idcardList.size());
            Long totalPageNum=(totalCount==0)?1:((totalCount%10==0)?totalCount/10:totalCount/10+1);
            map.put("totalCount", totalCount);
            map.put("totalPageNum", totalPageNum);
            map.put("list", idcardList);
            return map;
        }
        //3.按照起止日期查询

        if (startDate!=null&&!startDate.equals("")&&endDate!=null&&!endDate.equals("")) {
            List<Employee> hiredateList=employeeDao.selectHireDate(startDate, endDate);
            Long totalCount = Long.valueOf(hiredateList.size());
            Long totalPageNum=(totalCount==0)?1:((totalCount%10==0)?totalCount/10:totalCount/10+1);
            map.put("totalCount", totalCount);
            map.put("totalPageNum", totalPageNum);
            map.put("list", hiredateList);
            return map;
        }
        //4.按照状态查询

        if (status!=null&&!status.equals("")){
            List<Employee> statusList=employeeDao.selectStatus(status);
            Long totalCount = Long.valueOf(statusList.size());
            Long totalPageNum=(totalCount==0)?1:((totalCount%10==0)?totalCount/10:totalCount/10+1);
            map.put("totalCount", totalCount);
            map.put("totalPageNum", totalPageNum);
            map.put("list", statusList);
            return map;
        }
        //5.查询所有(也就是默认查询所有员工)
        List<Employee> list = employeeDao.selectCount();
        Long totalCount = employeeDao.selectEmployeeCount();
        Long totalPageNum=(totalCount==0)?1:((totalCount%10==0)?totalCount/10:totalCount/10+1);
        map.put("totalCount", totalCount);
        map.put("totalPageNum", totalPageNum);
        map.put("list", list);
        return map;
    }
}
